package com.wn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wn.dto.QueryTrainerDTO;
import com.wn.pojo.Trainer;
import com.wn.mapper.TrainerMapper;
import com.wn.service.ITrainerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wn.utils.PageData;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 培训师 服务实现类
 * </p>
 *
 * @author yellow docker
 * @since 2023-06-14
 */
@Service
public class TrainerServiceImpl extends ServiceImpl<TrainerMapper, Trainer> implements ITrainerService {

    /**
     * 分页步骤:
     * 插件配置过了
     * <p>
     * 1、组装Ipage
     * 2、调用 page 分页方法 (条件)
     * 3、拿到结果
     * 4、封装 pageData
     *
     * @param currentPage
     * @param pageSize
     * @param queryTrainerDTO
     * @return
     */
    @Override
    public PageData listSearch(Integer currentPage, Integer pageSize, QueryTrainerDTO queryTrainerDTO) {

        //条件
//        QueryWrapper<Trainer> qr = new QueryWrapper<>();

        LambdaQueryWrapper<Trainer> qr = Wrappers.lambdaQuery(Trainer.class);

        //模糊查询 name
        if (!StringUtils.isEmpty(queryTrainerDTO.getName())) {
            qr.like(Trainer::getName, queryTrainerDTO.getName());
        }
        //等级查询 level
        if (queryTrainerDTO.getLevel() != null && queryTrainerDTO.getLevel() != 0) {
            qr.eq(Trainer::getLevel, queryTrainerDTO.getLevel());
        }
        //入驻时间  createTimeStart
        if (queryTrainerDTO.getCreateTimeStart() != null) {
            qr.ge(Trainer::getCreateTime, queryTrainerDTO.getCreateTimeStart());
        }
        //结束时间  createTimeEnd
        if (queryTrainerDTO.getCreateTimeEnd() != null) {
            qr.le(Trainer::getCreateTime, queryTrainerDTO.getCreateTimeEnd());
        }

        //封装  分页的 currentPage\pageSize
        IPage<Trainer> page = new Page<>(currentPage,pageSize);

        //调用分页的 方法 (条件分页)
        IPage<Trainer> p = this.page(page, qr);

        //封装分页后的返回结果
        PageData pageData = new PageData();
        pageData.setTotal(p.getTotal());
        pageData.setData(p.getRecords());

        return pageData;
    }
}
